#/usr/bin/python
# coding: utf8

import time
import PAM30
import winsound
import sys
import win32api
import win32con

reload(sys)
sys.setdefaultencoding("utf-8")


def searchWd(sword, taoid, itemname):
    ie.navigate('http://www.taobao.com')
    ie.setTextBox('q', sword)
    ie.submitForm('search')
    #ie.clickButton('搜索')
    time.sleep(2)
    #idstr = 'id='+itemid
    #rt = ie.findText(idstr)
    #print taoid.encode('gbk')
    rt = ie.findText(taoid)
    if(rt):
        #找到淘宝id，然后点击商品链接
        print ("find "+taoid.encode('gbk'))
        #ie.clickLink(itemname)
        rtt = ie.findElement('a', 'title', itemname)

        #IE8要有个初始化设置，总是允许来自s.taobao.com的弹出窗口
        print rtt
        ie.clickElement(rtt)
        time.sleep(2)
        
        consis = str(rtt).find('tmall')
        if(consis == -1):
            ie.changeWindow(itemname+'-淘宝网')
        else:
            ie.changeWindow(itemname+'-tmall.com天猫')
        
        #print nie.outerHTML()
        #tr = ie.findText('桌子尺寸长1600*宽600*高70')
        #print tr
        #点击选项
        s1 = '白色1.6米'
        #s2 = '"送货到家+"<strong>60.00</strong>"元"'
        c1 = ie.findElement('span', 'innerText', s1)
        f = ie.clickElement(c1)
        print f
        #c2 = ie.findElement('li', 'title', s2)
        #ie.clickElement(c2)
        #购买
        buy = ie.findElement('div', 'id', 'J_juValid')
        a = ie.getElementChildren(buy)
        b = ie.getElementChildren(a[0])
        eleArry = ie.getElementChildren(buy)
        ie.clickElement(b[0])
        time.sleep(2)
    else:
        print 'not found'

ie= PAM30.PAMIE()
sword = u'时尚简约双人电脑桌书架组合 家用台式书桌书柜书橱办公桌'
taoid = '蓝月亮家具公司'
itemname = '时尚简约双人电脑桌书架组合 家用台式书桌书柜书橱办公桌'
#http://item.taobao.com/item.htm?id=15171926168
searchWd(sword, taoid, itemname)
winsound.Beep(370, 200)
